<template>
  <van-action-sheet v-model="isShow" title="频道管理">
    <div class="content">
      <div class="my">
        <span class="my-title">我的频道</span>
        <span class="my-btn" @click="edit = !edit">{{
          edit ? '完成' : '编辑'
        }}</span>
      </div>
      <van-grid>
        <van-grid-item v-for="(item, index) in list" :key="item.id">
          <span
            :class="{ active: index === value }"
            @click="itemClick(index)"
            >{{ item.name }}</span
          >
          <van-icon
            v-if="edit && item.name !== '推荐'"
            class="close"
            name="cross"
            @click="delItem(index)"
          />
        </van-grid-item>
      </van-grid>
      <div class="select">
        <span class="my-title">可选频道</span>
        <van-grid>
          <van-grid-item
            v-for="item in newList"
            :key="item.id"
            @click="selectClick(item)"
          >
            {{ item.name }}</van-grid-item
          >
        </van-grid>
      </div>
    </div>
  </van-action-sheet>
</template>

<script>
import { channels } from '@/api/home'
export default {
  props: {
    list: {
      type: Array,
      required: true
    },
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      isShow: false,
      allList: [],
      edit: false
    }
  },
  computed: {
    newList () {
      return this.allList.filter(item => {
        const result = this.list.find(i => i.id === item.id)
        return !result
      })
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await channels()
      this.allList = res.data.data.channels
    },
    selectClick (item) {
      this.$emit('setList', { method: 'push', value: item })
    },
    delItem (index) {
      this.$emit('setList', { method: 'splice', value: index })
      if (index < this.value) {
        this.$emit('input', this.value - 1)
      }
    },
    itemClick (index) {
      this.$emit('input', index)
      this.isShow = false
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  padding: 15px;
  .my {
    display: flex;
    align-items: center;
    justify-content: space-between;

    &-btn {
      color: #f00;
      border-radius: 3px;
      border: 1px solid #f00;
      padding: 6px;
    }
  }
  .my-title {
    font-size: 16px;
    font-weight: 500;
  }
  .select {
    padding-top: 20px;
  }
  .close {
    position: absolute;
    top: 3px;
    right: 3px;
  }
  .active {
    color: red;
  }
}
</style>
